package com.cy.letcode.string.重新调整字符串;

class Solution {

    public static void main(String[] args) {
        System.out.println(new Solution().reformat("covid2019"));
    }

    public String reformat(String s) {
        int length = s.length();
        if (length == 1) {
            return "";
        }
        Character[] chars = new Character[length * 2];
        int currentNumIndex = 0;
        int currentCharIndex = 1;
        int intNums = 0;
        int charNums = 0;
        for (int i = 0; i < length; i++) {
            if (s.charAt(i) >= 'a' && s.charAt(i) <= 'z') {
                charNums++;
                chars[currentCharIndex] = s.charAt(i);
                currentCharIndex += 2;
                continue;
            }
            if (s.charAt(i) >= '0' && s.charAt(i) <= '9') {
                intNums++;
                chars[currentNumIndex] = s.charAt(i);
                currentNumIndex += 2;
                continue;
            }
        }
        if (Math.abs(charNums - intNums) > 1) {
            return "";
        }

        String res = "";
        for (Character c : chars) {
            if (c == null) {
                continue;
            }
            res += c;
        }
        if (charNums > intNums) {
            res=res.charAt(res.length()-1)+res.substring(0,res.length()-1);
        }
        return res;
    }
}